﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;

using BeachRunner.Tileengine;
using BeachRunner.Utility;

namespace BeachRunner.BeachRunner
{
    /// <summary>
    /// Speichert die durch den Spieler gesammelten Wassertropfen
    /// 
    /// Autor: OLD (Sebastian Stiemer)
    /// </summary>
    public class WaterManager : TileOperator
    {
        public SoundEffect pointSound;

        // Grafiken für den Wasserbalken
        private Texture2D waterTexture0;
        private Texture2D waterTexture1;
        private Texture2D waterTexture2;
        private static short[][] indexes;
        private static short[][] times;
        private static AnimationSet animationSet;
        private Rectangle waterFillRec;
        private Vector2 waterBarPosition;
        Texture2D texture;
        private int waterSoundTimeleft;

        public WaterManager()
        {
            waterFillRec = new Rectangle(0, 0, 0, 46);
            waterBarPosition = new Vector2(Game1.RES_X - 378, 4);
        }

        public void LoadContent( ContentManager contentManager )
        {
            pointSound = contentManager.Load<SoundEffect>("sfx\\water");
            
            waterTexture0 = contentManager.Load<Texture2D>("gfx/lifebar/hp_0");
            waterTexture1 = contentManager.Load<Texture2D>("gfx/lifebar/hp_1");
            waterTexture2 = contentManager.Load<Texture2D>("gfx/lifebar/hp_2");

            indexes = new short[1][];
            indexes[0] = new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
                    11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
                    21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

            times = new short[1][];
            times[0] = new short[] { 70, 70, 70, 70, 70, 70, 70, 70, 
                    70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 
                    70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70 };

            texture = contentManager.Load<Texture2D>("gfx/lifebar/hp_3");

            animationSet = new AnimationSet(texture, 376, 46, indexes, times);
            animationSet.PlayAnimation();
        }

        /// <summary>
        /// die gesammelten Münzen im Spiel
        /// </summary>
        private int coins;

        public int Water
        {
            get { return coins; }
            set 
            {
                if (value < 0)
                {
                    this.coins = 0;
                }
                else
                {
                    this.coins = value;
                }
            }
        }


        /// <summary>
        /// lässt Water anwachsen
        /// </summary>
        public void AddWater()
        {
            if (Water < 100)
            {
                Water++;
            }
            else
            {
                Game1.ScoreManager.Scores += 2;
            }
        }

        /// <summary>
        /// stellt die Punkte auf dem Bildschirm da
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            waterFillRec.Width = this.Water * 4;
            spriteBatch.Draw(waterTexture0, waterBarPosition, Color.White);
            spriteBatch.Draw(waterTexture1, waterBarPosition, waterFillRec, Color.White);
            spriteBatch.Draw(waterTexture2, waterBarPosition, Color.White);
            spriteBatch.Draw(animationSet.Texture, waterBarPosition, 
                    new Rectangle(animationSet.CurrentRectangle.X, animationSet.CurrentRectangle.Y, this.Water * 4, animationSet.CurrentRectangle.Height), 
                    Color.White);
        }

        /// <summary>
        /// Hält die noch im Spiel befindlichen Münzen
        /// </summary>
        private Layer pointsLayer;
        /// <summary>
        /// Macht die noch im Spiel befindlichen Münzen zugänglich
        /// </summary>
        public Layer PointsLayer
        {
            get { return pointsLayer; }
        }

        /// <summary>
        /// Setzt die im Spiel befindlichen Münzen zurück ( alle Münzen wieder da )
        /// </summary>
        /// <param name="map">Die zu verwendende Map</param>
        public void Reset(Map map)
        {
            int indexLayerPoints;
            map.NameToLayer.TryGetValue("points", out indexLayerPoints);

            Reset(map.Layer[indexLayerPoints]);
        }

        /// <summary>
        /// Setzt die im Spiel befindlichen Münzen zurück ( alle Münzen wieder da )
        /// </summary>
        /// <param name="layer">Das zu verwendende Layer</param>
        public void Reset(Layer layer)
        {
            this.pointsLayer = new Layer(layer);
        }

        private const int INDEX_POINT_TILE = 131; 

        public void Operate(int x, int y)
        {
            int index = y * pointsLayer.Width + x;
            if (index < 0 || index >= pointsLayer.Data.Length)
            {
                return;
            }

            if (pointsLayer.Data[index] == INDEX_POINT_TILE)
            {
                pointsLayer.Data[index] = 0;
                Game1.WaterManager.AddWater();

                if (Game1.Config.Sound_effects && waterSoundTimeleft == 0)
                {
                    pointSound.Play();
                    // Maximal 2 Punktesounds zur gleichen Zeit abspielen
                    waterSoundTimeleft = pointSound.Duration.Milliseconds / Game1.TIME_PER_TICK / 2;
                }
                    
            }
        }

        public void Update()
        {
            animationSet.Update();
            if (waterSoundTimeleft != 0)
                waterSoundTimeleft--;
        }
    }
}
